/*
 * Copyright (C) 2012 Damien Girard <dgirard@nativesoft.fr>
 * Copyright (C) 2012 Sebastien Baudrand <sebbaudrand@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fr.umlv.qroxy.communication.transfer;

/**
 * Calculate the speed of a communication.
 * 
 * @author Damien Girard <dgirard@nativesoft.fr>
 * @author Sebastien Baudrand <sebbaudrand@gmail.com>
 */
public class SpeedCalculator {

    private long previousTime = 0;
    private long previousAmountOfData = 0;
    private double previousSpeed = -1;

    /**
     * Calcul the speed of the connection
     * 
     * @param newAmountOfData
     * @param newTime
     * @return 
     */
    public double updateSpeed(long newAmountOfData, long newTime) {
        // Calculating the new speed.
        if (newTime - previousTime < 1000) {
            return -1;
        }
        
        double speed = (newAmountOfData - previousAmountOfData) / (newTime - previousTime);
        
        this.previousAmountOfData = newAmountOfData;
        this.previousTime = newTime;

        previousSpeed = speed;

        return previousSpeed;
    }

    public double getSpeed() {
        return previousSpeed;
    }

    public double getPreviousSpeed() {
        return previousSpeed;
    }
    
    
    
}
